package gov.pbc.nn.kjgl.archive.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.ParseResult;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import gov.pbc.nn.core.dao.AbstractJdbcDao;
import gov.pbc.nn.kjgl.entity.Archives;
import gov.pbc.nn.kjgl.entity.ArchivesContent;

@Repository
public class ArchivesJdbcDao extends AbstractJdbcDao{

//	@Autowired
//	private ArchivesContentDao archivesContentDao;
	/**
	 * 支持表格的ShowfilterBar属性的 2表 查询 
	 * @param page
	 * @param criteria
	 */
	public void  loadArchivesContent(Page<ArchivesContent> page, Criteria criteria){
		String sql = "SELECT\n" +
				"			* \n" +
				"		FROM \n" +
				"		( \n" +
				"		SELECT \n" +
				"					C.*, CONCAT( \n" +
				"			CASE \n" +
				"			WHEN A2.ID_ IS NULL THEN \n" +
				"			'' \n" +
				"			ELSE \n" +
				"				CONCAT(A2.NAME_,'/')\n" +
				"			END \n" +
				"	 ,A.NAME_ )AS NAME_ \n" +
				"				FROM \n" +
				"				T_ARCHIVES_CONTENT C \n" +
				"				INNER JOIN T_ARCHIVES A ON C.ARCHIVES_ID_ = A.ID_ \n" +
				"		INNER JOIN T_ARCHIVES_MEMBER M ON M.ARCHIVES_ID_ = A.ID_ \n" +
				"			LEFT JOIN T_ARCHIVES A2 ON A2.ID_ = A.PARENT_ID_ \n" +
				"				AND M.INVALID_ = 0 \n" +
				"			AND M.TYPE_ = 0 \n" +
				"				WHERE \n" +
				"				 M.INVALID_ = 0 AND	A.INVALID_ = 0 \n" +
				"				AND C.INVALID_ = 0 \n" +
				"				AND M.MEMBER_ID_ =:deptId \n" +
				"		) X";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("deptId", ContextHolder.getLoginUser().getDepts().get(0).getId());
		ParseResult pr = this.parseCriteria(criteria, true, "x");
		if (pr != null) {
			StringBuffer sbSql = pr.getAssemblySql();
			sql += " and " + sbSql.toString();
			params.putAll(pr.getValueMap());
		}
		this.pagingQuery(page, sql,new RowMapper<ArchivesContent>() {
			@Override
			public ArchivesContent mapRow(ResultSet rs, int rowNum) throws SQLException {
				// TODO Auto-generated method stub
				ArchivesContent arc=new ArchivesContent();
				arc.setArchivesId(rs.getString("ARCHIVES_ID_"));
				arc.setContentFileId(rs.getString("CONTENT_FILE_ID_"));
				arc.setPathId(rs.getString("PATH_ID_"));
				arc.setDeptId(rs.getString("DEPT_ID_"));
				arc.setInputUser(rs.getString("INPUT_USER_"));
				arc.setInputUsername(rs.getString("INPUT_USERNAME_"));
				arc.setCreateUser(rs.getString("CREATE_USER_"));
				arc.setCreateDate(rs.getDate("CREATE_DATE_"));
				arc.setUpdateDate(rs.getDate("UPDATE_DATE_"));
				arc.setUpdateUser(rs.getString("UPDATE_USER_"));
				arc.setInvalid(rs.getBoolean("INVALID_"));
				arc.setId(rs.getString("ID_"));
				arc.setRemark(rs.getString("REMARK_"));
				arc.setDeptName(rs.getString("DEPT_NAME_"));
				try {
				arc=EntityUtils.toEntity(arc);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				EntityUtils.setValue(arc, "name",rs.getString("NAME_"));
				return arc;
			}
		},params);
	}
	
}
